package ali

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"oss-uploader/lib"
)

const (
	connectTimeoutSec int64 = 60
	readWriteTimeout  int64 = 120
)

var (
	bucket *oss.Bucket
	client *oss.Client
)

func Conn() {
	var err error
	client, err = oss.New(lib.AliCfg.Endpoint, lib.AliCfg.AccessKey, lib.AliCfg.SecretKey, oss.Timeout(connectTimeoutSec, readWriteTimeout))
	lib.HandleError(err)

	bucket, err = client.Bucket(lib.AliCfg.Bucket)
	lib.HandleError(err)
}

func UploadFile(objectKey, filePath string) {
	if err := bucket.PutObjectFromFile(objectKey, filePath); err != nil {
		fmt.Println(err)
	}
}
